package org.musicguardian.core.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

public class Library implements java.io.Serializable, Comparable<Library> {
	private String name;

	private TreeSet<Artist> artists = new TreeSet<Artist>();

	public Library() {
		super();
	}

	public Library(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public Library addArtist(Artist artist) {
		if (!artists.add(artist)) {
			Artist knownArtist = artists.tailSet(artist).first();
			for (Iterator<Album> iter = artist.getAlbums().iterator(); iter
					.hasNext();) {
				knownArtist.addAlbum(iter.next());
			}
		}
		return this;
	}

	public TreeSet<Artist> getArtists() {
		return artists;
	}

	@Override
	public String toString() {
		String string = "";
		for (Iterator<Artist> iter = artists.iterator(); iter.hasNext();) {
			Artist artist = iter.next();
			string += "\n" + artist.toString();
		}
		return string;
	}

	public int compareTo(Library library) {
		return getName().compareTo(library.getName());
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Library other = (Library) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
